package com.milaidoo.fudong;


import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();
    private ServiceConnection mServiceConnection;
    private FloatWinfowServices.MyBinder mLocalBinder;
    private EditText etContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initBinder(){
        if(mServiceConnection != null){
            return;
        }
        mServiceConnection = new ServiceConnection(){
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {

                mLocalBinder = (FloatWinfowServices.MyBinder)service;
//                //client得到服务LocalService的句柄，便可以调用服务里面声明的public方法funLocalService()
//                mLocalBinder.setScrollSpeed(1f);
                mLocalBinder.getService().setOnServiceListener(new FloatWinfowServices.OnServiceListener() {
                    @Override
                    public void onClosed() {
                        unbindService(mServiceConnection);
                        mServiceConnection = null;
                        mLocalBinder = null;
                    }
                });
                Log.d(TAG, "LocalService is connected");
                //
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                Log.d(TAG, "LocalService is disconnected");
                mServiceConnection = null;
                mLocalBinder = null;
            }
        };
    }

    private void initView(){
        initBinder();
        etContent = findViewById(R.id.et_content);
        findViewById(R.id.tv_test).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bind();
            }
        });

        findViewById(R.id.tv_reset_content).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mLocalBinder == null || !mLocalBinder.isBinderAlive() || mServiceConnection == null){
                    Toast.makeText(MainActivity.this,"请先打开窗口",Toast.LENGTH_LONG).show();
                    return;
                }
                if(TextUtils.isEmpty(etContent.getText().toString().trim())){
                    Toast.makeText(MainActivity.this,"请输入需要播放的内容！！！",Toast.LENGTH_LONG).show();
                    return;
                }
                mLocalBinder.setText(etContent.getText().toString().trim(),60,Color.WHITE);
//                mLocalBinder.setContent(etContent.getText().toString().trim(),60, Color.parseColor("#ffffff"));
            }
        });

        findViewById(R.id.tv_test1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("yqy","tv_test1");
            }
        });
    }

    private void bind(){
        initBinder();
        Intent bindIntent = new Intent(this.getApplicationContext(),FloatWinfowServices.class);
        bindService(bindIntent, mServiceConnection, Context.BIND_AUTO_CREATE);
    }
}